//
//  carouselView.m
//  Leisure
//
//  Created by lanou on 16/4/21.
//  Copyright © 2016年 lanou. All rights reserved.
//

#define kWidth self.frame.size.width
#define kHeight self.frame.size.height

#import "carouselView.h"
#import "NSTimer+Addition.h"
#import <UIImageView+WebCache.h>


@interface carouselView ()<UIScrollViewDelegate>

// 最下面的scrollView
@property (nonatomic, strong) UIScrollView *scrollerView;
// 网址数组
@property (nonatomic, strong) NSArray *imageUrls;
// 标题数组
@property (nonatomic, strong) NSArray *textArr;
@property (nonatomic, strong) UIPageControl *page;
// 定时器
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation carouselView

- (instancetype)initWithFrame:(CGRect)frame imageUrls:(NSArray *)imageUrls textArr:(NSArray *)textArr{
    self = [super initWithFrame:frame];
    if (self) {
        if (imageUrls.count == 0) {
            return nil;
        }
        self.imageUrls = imageUrls;
        self.textArr = textArr;
        [self crollerView];
        if (textArr == nil) {
            [self initImages];
        } else {
        [self initImagesAndText];
        }
        [self creatPageControl];
        if (self.imageUrls.count > 1) {
            [self creatTime];
        }
    }
    return self;
}

- (void)creatPageControl {
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(kWidth - 100, kHeight - 20, 100, 20)];
    [self addSubview:self.page];
    // 一共有几个点
    self.page.numberOfPages = self.imageUrls.count;
}

- (void)crollerView {
    self.scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    self.scrollerView.contentSize = CGSizeMake(kWidth * self.imageUrls.count, 0);
    self.scrollerView.showsHorizontalScrollIndicator = NO; // 隐藏水平滚动条
    self.scrollerView.pagingEnabled = YES; // 一次滚动一页
    self.scrollerView.delegate = self;
    self.scrollerView.contentOffset = CGPointMake(kWidth, 0);
    [self addSubview:self.scrollerView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint currentPoint = scrollView.contentOffset; // 得到当前的偏移量
    if (currentPoint.x <= kWidth / 2) {
        scrollView.contentOffset = CGPointMake(self.imageUrls.count * kWidth + currentPoint.x, 0);
    } else if (currentPoint.x > (self.imageUrls.count + 0.5) * kWidth) {
        scrollView.contentOffset = CGPointMake(currentPoint.x - self.imageUrls.count * kWidth, 0);
    }
    //PageControl随轮播图偏移
    self.page.currentPage = self.scrollerView.contentOffset.x / kWidth - 1;
}
// 图片加文字
- (void)initImagesAndText {
    // 如果只有一个网址不用循环轮播 可以自己创建一张图片
    if (self.imageUrls.count == 1) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls[0]]];
        [self.scrollerView addSubview:imageView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClickFuncation)];
        [imageView addGestureRecognizer:tap];
        imageView.userInteractionEnabled = YES;
        return;
    }
    // 在左右两边各添加一张imageView
    self.scrollerView.contentSize = CGSizeMake(kWidth * (self.imageUrls.count + 2), 0);
    for (int i = 0; i < self.imageUrls.count
         + 2; i++)
    {
        // 点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClickFuncation)];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * kWidth, 0, kWidth, kHeight)];
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 120, 300, 21)];
        title.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
        title.textColor = [UIColor whiteColor];
        title.shadowColor = [UIColor blackColor];
        // 给每张图片 添加点击事件
        [imageView addGestureRecognizer:tap];
        // 打开交互
        imageView.userInteractionEnabled = YES;
        // 第一张显示最后一张内容
        if (i == 0) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls.lastObject]];
            title.text = self.textArr.lastObject;
            [imageView addSubview:title];
            // 最后一张显示第一张内容
        } else if (i == self.imageUrls.count + 1) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls.firstObject]];
            title.text = self.textArr.firstObject;
            [imageView addSubview:title];
        } else {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls[i - 1]]];
            title.text = self.textArr[i - 1];
            [imageView addSubview:title];
        }
        
        [self.scrollerView addSubview:imageView];
    }
}

// 图片
- (void)initImages {
    // 如果只有一个网址不用循环轮播 可以自己创建一张图片
    if (self.imageUrls.count == 1) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls[0]]];
        [self.scrollerView addSubview:imageView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClickFuncation)];
        [imageView addGestureRecognizer:tap];
        imageView.userInteractionEnabled = YES;
        return;
    }
    // 在左右两边各添加一张imageView
    self.scrollerView.contentSize = CGSizeMake(kWidth * (self.imageUrls.count + 2), 0);
    for (int i = 0; i < self.imageUrls.count
         + 2; i++)
    {
        // 点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClickFuncation)];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * kWidth, 0, kWidth, kHeight)];
        // 给每张图片 添加点击事件
        [imageView addGestureRecognizer:tap];
        // 打开交互
        imageView.userInteractionEnabled = YES;
        // 第一张显示最后一张内容
        if (i == 0) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls.lastObject]];
            // 最后一张显示第一张内容
        } else if (i == self.imageUrls.count + 1) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls.firstObject]];
        } else {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls[i - 1]]];
        }

        [self.scrollerView addSubview:imageView];
    }
}

// 图片点击
- (void)imageClickFuncation {
    // 回调block
    if (self.imageClick) {
        self.imageClick(self.page.currentPage);
    }
}


- (void)creatTime {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerHander) userInfo:nil repeats:YES];
}

- (void)timerHander {
    CGPoint newOffset = CGPointMake(_scrollerView.contentOffset.x + kWidth, 0);
    [_scrollerView setContentOffset:newOffset animated:YES];
   
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView // 开始拖拽的时候会触发这个⽅法
{
    [_timer pauseTimer];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView // 当结束拖拽的时候会触发这个⽅法
{
    [_timer resumeTimerAfterTimeInterval:5];
}


@end
